package dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.ArrayHandler;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
*/

import dbPool.ConnectionDAO;
import beans.Customer;
import beans.CustomerLogin;

public class CustomerLoginDAO {
	
	ConnectionDAO conn = new ConnectionDAO();
    Connection con = null;
    Statement stmt = null;
    
    static {
    	try{
    		new dbPool.JDCConnectionDriver("com.mysql.jdbc.Driver","jdbc:mysql://localhost/virtualreel", "root", "root"); 
    	}catch(Exception e){
    		
    	}
    }
    
    public Connection getConnection() throws SQLException {
        return DriverManager.getConnection("jdbc:jdc:jdcpool");
    }
    
    public int createCustomerLogin(CustomerLogin cBean){
    	try{
    		con = getConnection();
    		Statement stmt = con.createStatement();
    		String query = "INSERT INTO `customerlogin`(c_id, c_username, c_password, c_security_question, c_security_answer) VALUES('"+cBean.getC_Id()+"','"+cBean.getC_Username()+"','"+cBean.getC_Password()+"','"+cBean.getC_Security_Question()+"','"+cBean.getC_Security_Answer()+"');";
    		stmt.executeUpdate(query);
    		return 0;
    	}
    	catch(Exception se){
    		Logger.getLogger(CustomerLoginDAO.class.getName()).log(Level.SEVERE, "save failed", se);
    		return 1;
    	}
    	finally {
    		try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	}
    }
    
    public CustomerLogin retrieveCustomer(CustomerLogin cBean, String Column_Name){
    	try{
    		con = getConnection();
    		Statement stmt = con.createStatement();
    		CustomerLogin cBeanOne = new CustomerLogin();
    		String query = "SELECT "+Column_Name+" FROM `customerlogin` WHERE c_id LIKE "+cBean.getC_Id()+";";
    		ResultSet rs = stmt.executeQuery(query);
    		while(rs.next()){
    			cBeanOne.setC_Id(rs.getString("C_Id"));
    			cBeanOne.setC_Username(rs.getString("C_Username"));
    			cBeanOne.setC_Password(rs.getString("C_Password"));
    			cBeanOne.setC_Security_Question(rs.getString("C_Security_Question"));
    			cBeanOne.setC_Security_Answer(rs.getString("C_Security_Answer"));
    		}
    		return cBeanOne;
    	}
    	catch(Exception se){
    		Logger.getLogger(CustomerLoginDAO.class.getName()).log(Level.SEVERE, "save failed", se);
    		return cBean;
    	}
    	finally {
    		try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	}
    }
    
    public int updateCustomer(CustomerLogin cBean, String key, String value){
    	try{
    		con = getConnection();
    		Statement stmt = con.createStatement();
    		String query = "UPDATE `customerlogin` SET  `"+key+"` =  '"+value+"' WHERE  `customer`.`c_id` =  '"+cBean.getC_Id()+"';";
    		stmt.executeUpdate(query);
    		return 0;
    	}
    	catch(Exception se){
    		Logger.getLogger(CustomerLoginDAO.class.getName()).log(Level.SEVERE, "save failed", se);
    		return 1;
    	}
    	finally {
    		try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	}
    }
    
    public int deleteCustomer(CustomerLogin cBean) {
    	try{
    		con = getConnection();
    		Statement stmt = con.createStatement();
    		String query = "DELETE FROM `customerlogin` WHERE `customerlogin`.`c_id` = '"+cBean.getC_Id()+"';";
    		stmt.executeUpdate(query);
    		return 0;
    	}
    	catch(Exception se){
    		Logger.getLogger(CustomerLoginDAO.class.getName()).log(Level.SEVERE, "save failed", se);
    		return 1;
    	}
    	finally {
    		try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	}
    }
    
    public String authenticateCustomer(String username, String password){
    	try{
    		con = getConnection();
    		Statement stmt = con.createStatement();
    		String match = ""; String cid="";
    		String query = "SELECT * FROM `customerlogin` WHERE `c_username` = '"+username+"';";
    		ResultSet rs = stmt.executeQuery(query);
    		//while(rs.next()){
    			rs.next();
    			match = rs.getString("c_password");
    		//}
    		
    		if(match.equals(password)){
    			cid=rs.getString("c_id");
    		}
    		
    		return cid;
    	}
    	catch(Exception se){
    		Logger.getLogger(CustomerLoginDAO.class.getName()).log(Level.SEVERE, "save failed", se);
    		return "authentication error";
    	}
    	finally {
    		try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	}
    }

}
